<?php
class PackageController extends CBaseController
{
	
	protected $package;
	public function __construct(&$config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
		$this->prepare();
		if(is_file(dirname(__FILE__).'/PackageSlotProvider.php'))
		{
			include(dirname(__FILE__).'/PackageSlotProvider.php');
			$slotProvider=PackageSlotProvider::getInstance($this->package);
			$this->templator->assignByRef('cms',$slotProvider);
		}
		
	}
	
	private function prepare()
	{
		if(isset($_GET['id']))
		{
			$id = $_GET['id'];
			$this->package = R::findOne('travelpackage',' seoname = ? ',array($id));
		}
		elseif(isset($_POST['id']))
		{	
			$id = $_POST['id'];
			$this->package = R::findOne('travelpackage',' seoname = ? ',array($id));
		}
		else
		{
			$this->package = R::dispense('travelpackage');
		}
		if(!isset($this->package))
		{
			$this->package = R::dispense('travelpackage');
		}
		
	}	
	
	public function details()
	{
		$this->templator->assignByRef('package',$this->package);
		$image=R::relatedOne($this->package, 'image' );
		$facilities=R::related($this->package, 'facility' );		
		$defaultOption=R::findOne('pckgOption'
			,'travelpackage_id=:package_id order by position limit 1'
			,array(':package_id'=>$this->package->id));
		
		$hotels=array();
		if(isset($defaultOption))
		{
			$this->templator->assignByRef('defaultOption',$defaultOption);		
			$ref=$defaultOption->ownHotel_travelpackage;
			foreach ($ref as $id =>$entity)
			{
				$hotel=$entity->hotel;
				$hotel->days=$entity->days;
				$hotel->mealplan=$entity->mealplan;
				$hotel->nights=$entity->nights;
				$hotels[]=$hotel;
			}
		}
		$this->templator->assignByRef('hotels',$hotels);
		$this->templator->assign('facilities',array_merge($facilities, array()));
		$this->templator->assign('exclusions',array_merge($this->package->ownExclusion, array()));
		$this->templator->assign('inclusions',array_merge($this->package->ownInclusion, array()));
		$this->templator->assign('pckgOptions',array_merge($this->package->ownPckgOption, array()));
		$this->templator->assignByRef('image',$image);
		$this->templator->display('package_details.html');		
	}
}	